package com.imyuanma.qingyun.common.client.ums;

import com.imyuanma.qingyun.common.config.security.model.SsoAuthenticationToken;
import com.imyuanma.qingyun.interfaces.ums.model.LoginUserDTO;
import com.imyuanma.qingyun.interfaces.ums.model.SsoVerifyDTO;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 登录用户工具类
 *
 * @author wangjy
 * @date 2022/07/16 17:04:45
 */
public class LoginUserHolder {
    /**
     * 获取当前登录用户
     *
     * @return 当前登录用户信息
     */
    public static LoginUserDTO getLoginUser() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        if (securityContext != null && securityContext.getAuthentication() instanceof SsoAuthenticationToken) {
            SsoAuthenticationToken ssoAuthenticationToken = (SsoAuthenticationToken) securityContext.getAuthentication();
            SsoVerifyDTO ssoVerifyDTO = ssoAuthenticationToken.getSsoVerifyDTO();
            if (ssoVerifyDTO != null) {
                return ssoVerifyDTO.getLoginUser();
            }
        }
        return null;
    }
}
